package com.xiaoshuidi.cloud.module.iot.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.module.iot.dal.model.callback.YunDingDeviceCallbackReqVO;
import com.xiaoshuidi.cloud.module.iot.dal.model.callback.YunDingDeviceEventReqVO;
import com.xiaoshuidi.cloud.module.iot.dal.model.device.TbDeviceDosageCallbackModel;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceDo;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceDosageDo;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDoorlockPassword;
import com.xiaoshuidi.cloud.module.iot.dal.repository.DeviceRepository;
import com.xiaoshuidi.cloud.module.iot.dal.repository.TbDoorLockPasswordRepository;
import com.xiaoshuidi.cloud.module.iot.enums.BrandEnum;
import com.xiaoshuidi.cloud.module.iot.enums.RedisTypeEnum;
import com.xiaoshuidi.cloud.module.iot.service.CacheService;
import com.xiaoshuidi.cloud.module.iot.service.LocalCallbackService;
import com.xiaoshuidi.cloud.module.iot.service.YunDingCallbackService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author Carl
 * create 2020/7/21
 * email Carl.Sun@ixiaoshuidi.com
 **/
@Service
public class YunDingCallbackServiceImpl implements YunDingCallbackService {

    private Logger LOGGER = LoggerFactory.getLogger(YunDingCallbackServiceImpl.class);

    @Autowired
    private LocalCallbackService localCallbackService;

    @Autowired
    private DeviceRepository deviceDataAccess;

    @Autowired
    private TbDoorLockPasswordRepository doorLockPasswordDataAccess;

    @Autowired
    private CacheService cacheService;

    /**
     * 云丁回调处理
     *
     * @param yunDingDeviceCallbackReqVO
     */
    @Override
    public void disposeCallback(YunDingDeviceCallbackReqVO yunDingDeviceCallbackReqVO) throws Exception {
        String service = yunDingDeviceCallbackReqVO.getService();
        JSONObject result = yunDingDeviceCallbackReqVO.getResult();
        String setviceId = yunDingDeviceCallbackReqVO.getServiceid();
        String uuid = yunDingDeviceCallbackReqVO.getUuid();
        LOGGER.info("云丁回调请求参数：{}", JSONObject.toJSONString(yunDingDeviceCallbackReqVO));
        switch (service) {
            //电表操作回调
            case "Elemeter_Control_Service":
                this.disposeElementControlService(uuid, result);
                break;
            //zigbee电表跳闸回调
            case "Zmeter_Open_Service":
                this.disposeZmeterOpenService(uuid, result);
                break;
            //zigbee电表合闸回调
            case "Zmeter_Close_Service":
                this.disposeZmeterCloseService(uuid, result);
                break;
            //门锁密码增加回调
            case "Password_Add_Service":
                this.disposeDoorLockPasswordAddService(uuid, result);
                break;
            //门锁密码修改回调
            case "Password_Update_Service":
                this.disposeDoorLockPasswordUpdateService(uuid, setviceId, result);
                break;
            //门锁密码删除回调
            case "Password_Delete_Service":
                this.disposeDoorLockPasswordDeleteService(uuid, result);
                break;
            //门锁密码冻结回调
            case "Password_Frozen_Service":
                this.disposeDoorLockPasswordFrozenService(uuid, result);
                break;
            //门锁密码解冻回调
            case "Password_Unfrozen_Service":
                this.disposeDoorLockPasswordUnFrozenService(uuid, result);
                break;
            default:
                break;
        }

    }

    /**
     * zigbee电表合闸回调
     *
     * @param uuid
     * @param result
     */
    private void disposeZmeterCloseService(String uuid, JSONObject result) throws Exception {
        // 错误码，1000：成功，其他：错误
        int errNo = result.getIntValue("ErrNo");
        //操作类型，7：合闸
        int operation_type = result.getIntValue("operation_type");
        this.disposeDevicePowerStatus(uuid, operation_type, errNo);
    }

    /**
     * zigbee电表跳闸回调
     *
     * @param uuid
     * @param result
     */
    private void disposeZmeterOpenService(String uuid, JSONObject result) throws Exception {
        // 错误码，1000：成功，其他：错误
        int errNo = result.getIntValue("ErrNo");
        //操作类型，6：跳闸；
        int operation_type = result.getIntValue("operation_type");
        this.disposeDevicePowerStatus(uuid, operation_type, errNo);
    }

    /**
     * 处理 电表操作回调
     *
     * @param uuid   云丁设备唯一编号
     * @param result
     */
    private void disposeElementControlService(String uuid, JSONObject result) throws Exception {
        // 错误码，1000：成功，其他：错误
        int errNo = result.getIntValue("ErrNo");
        //操作类型，1：充电；2：设置透支额度；3：电表清零；4：设置最大功率；6：跳闸；7：合闸
        int operation_type = result.getIntValue("operation_type");
        switch (operation_type) {
            case 6:
            case 7:
                this.disposeDevicePowerStatus(uuid, operation_type, errNo);
                break;
            default:
                break;

        }

    }

    /**
     * 云丁电表合闸跳闸处理
     *
     * @param uuid
     * @param operation_type
     * @param errNo
     */
    private void disposeDevicePowerStatus(String uuid, int operation_type, int errNo) throws Exception {
        //6：跳闸；7：合闸
        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setDeviceThirdNo(uuid);
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        if (1000 != errNo) {
            //跳闸失败，则当前状态即是合闸
            if (6 == operation_type) {
                tbDeviceDo.setPowerStatus(1);
            } else {
                //合闸失败,则当前状态就是跳闸
                tbDeviceDo.setPowerStatus(0);
            }
        }
        localCallbackService.updateTbDeviceRecord(tbDeviceDo);
    }

    /**
     * 云丁门锁新增处理
     *
     * @param uuid
     * @param result
     * @throws Exception
     */
    private void disposeDoorLockPasswordAddService(String uuid, JSONObject result) throws Exception {
        // 错误码，1000：成功，其他：错误
        String errNo = result.getString("ErrNo");
        String passwordId = result.getString("id");
        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setDeviceThirdNo(uuid);
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        tbDeviceDo = deviceDataAccess.getTbDeviceByConditon(tbDeviceDo);
        if (tbDeviceDo != null) {
            TbDoorlockPassword tbDoorlockPassword = new TbDoorlockPassword();
            tbDoorlockPassword.setDeviceNo(tbDeviceDo.getDeviceNo());
            tbDoorlockPassword.setThirdPasswordId(passwordId);
            tbDoorlockPassword.setStatus(1);
            tbDoorlockPassword = doorLockPasswordDataAccess.selectTbDoorlockPassword(tbDoorlockPassword);
            TbDoorlockPassword tbDoorlockPasswordNow = new TbDoorlockPassword();
            tbDoorlockPasswordNow.setId(tbDoorlockPassword.getId());
            tbDoorlockPasswordNow.setUpdateTime(LocalDateTime.now());
            if (errNo.equals("1000")) {
                //操作成功，密码状态改为生效中
                tbDoorlockPasswordNow.setPasswordStatus(1);
            } else {
                //操作失败，密码状态改为添加失败
                tbDoorlockPasswordNow.setPasswordStatus(5);
            }
            doorLockPasswordDataAccess.updateByPrimaryKeySelective(tbDoorlockPasswordNow);
        }
    }

    /**
     * 云丁门锁修改处理
     *
     * @param uuid
     * @param result
     * @throws Exception
     */
    private void disposeDoorLockPasswordUpdateService(String uuid, String setviceId, JSONObject result) throws Exception {
        // 错误码，1000：成功，其他：错误
        String errNo = result.getString("ErrNo");
        Integer passwordId = result.getInteger("id");
        //从缓存中取出tbDoorlockPassword对象，根据第三方请求的状态去修改本地的状态
        String doorlockValue = cacheService.getString(RedisTypeEnum.SDIOT_DOORLOCK_ROLLBACK.getCode() + setviceId);
        LOGGER.info("云丁-门锁普通密码取出缓存，返回结果:{}", doorlockValue);
        if (StringUtils.isNotBlank(doorlockValue)) {
            TbDoorlockPassword tbDoorlockPassword = JSONObject.parseObject(doorlockValue, TbDoorlockPassword.class);
            TbDoorlockPassword tbDoorlockPasswordNow = new TbDoorlockPassword();
            tbDoorlockPasswordNow.setDeviceNo(tbDoorlockPassword.getDeviceNo());
            tbDoorlockPasswordNow.setThirdPasswordId(tbDoorlockPassword.getThirdPasswordId());
            tbDoorlockPasswordNow.setStatus(1);
            tbDoorlockPasswordNow = doorLockPasswordDataAccess.selectTbDoorlockPassword(tbDoorlockPasswordNow);
            if (tbDoorlockPasswordNow != null) {
                if (errNo.equals("1000")) {
                    //修改成功重新定义对象修改状态
                    tbDoorlockPassword = new TbDoorlockPassword();
                    tbDoorlockPassword.setId(tbDoorlockPasswordNow.getId());
                    tbDoorlockPassword.setPasswordStatus(1);
                    tbDoorlockPassword.setUpdateTime(LocalDateTime.now());
                    doorLockPasswordDataAccess.updateByPrimaryKeySelective(tbDoorlockPassword);
                } else {
                    //失败就直接回滚
                    doorLockPasswordDataAccess.updateById(tbDoorlockPassword);
                }
            }
        }
    }

    /**
     * 云丁门锁冻结处理
     *
     * @param uuid
     * @param result
     * @throws Exception
     */
    private void disposeDoorLockPasswordFrozenService(String uuid, JSONObject result) throws Exception {
        // 错误码，1000：成功，其他：错误
        String errNo = result.getString("ErrNo");
        String passwordId = result.getString("id");
        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setDeviceThirdNo(uuid);
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        tbDeviceDo = deviceDataAccess.getTbDeviceByConditon(tbDeviceDo);
        if (tbDeviceDo != null) {
            TbDoorlockPassword tbDoorlockPassword = new TbDoorlockPassword();
            tbDoorlockPassword.setDeviceNo(tbDeviceDo.getDeviceNo());
            tbDoorlockPassword.setThirdPasswordId(passwordId);
            tbDoorlockPassword.setStatus(1);
            tbDoorlockPassword = doorLockPasswordDataAccess.selectTbDoorlockPassword(tbDoorlockPassword);
            TbDoorlockPassword tbDoorlockPasswordNow = new TbDoorlockPassword();
            tbDoorlockPasswordNow.setId(tbDoorlockPassword.getId());
            tbDoorlockPasswordNow.setUpdateTime(LocalDateTime.now());
            if (errNo.equals("1000")) {
                //操作成功，密码状态改为冻结
                tbDoorlockPasswordNow.setPasswordStatus(0);
            } else {
                //操作失败，密码状态改为生效中
                tbDoorlockPasswordNow.setPasswordStatus(1);
            }
            doorLockPasswordDataAccess.updateByPrimaryKeySelective(tbDoorlockPasswordNow);
        }
    }

    /**
     * 云丁门锁解冻处理
     *
     * @param uuid
     * @param result
     * @throws Exception
     */
    private void disposeDoorLockPasswordUnFrozenService(String uuid, JSONObject result) throws Exception {
        // 错误码，1000：成功，其他：错误
        String errNo = result.getString("ErrNo");
        String passwordId = result.getString("id");
        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setDeviceThirdNo(uuid);
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        tbDeviceDo = deviceDataAccess.getTbDeviceByConditon(tbDeviceDo);
        if (tbDeviceDo != null) {
            TbDoorlockPassword tbDoorlockPassword = new TbDoorlockPassword();
            tbDoorlockPassword.setDeviceNo(tbDeviceDo.getDeviceNo());
            tbDoorlockPassword.setThirdPasswordId(passwordId);
            tbDoorlockPassword.setStatus(1);
            tbDoorlockPassword = doorLockPasswordDataAccess.selectTbDoorlockPassword(tbDoorlockPassword);
            TbDoorlockPassword tbDoorlockPasswordNow = new TbDoorlockPassword();
            tbDoorlockPasswordNow.setId(tbDoorlockPassword.getId());
            tbDoorlockPasswordNow.setUpdateTime(LocalDateTime.now());
            if (errNo.equals("1000")) {
                //操作成功，密码状态改为生效中
                tbDoorlockPasswordNow.setPasswordStatus(1);
            } else {
                //操作失败，密码状态改为冻结
                tbDoorlockPasswordNow.setPasswordStatus(0);
            }
            doorLockPasswordDataAccess.updateByPrimaryKeySelective(tbDoorlockPasswordNow);
        }
    }

    /**
     * 云丁门锁删除处理
     *
     * @param uuid
     * @param result
     * @throws Exception
     */
    private void disposeDoorLockPasswordDeleteService(String uuid, JSONObject result) throws Exception {
        // 错误码，1000：成功，其他：错误
        String errNo = result.getString("ErrNo");
        String passwordId = result.getString("id");
        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setDeviceThirdNo(uuid);
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        tbDeviceDo = deviceDataAccess.getTbDeviceByConditon(tbDeviceDo);
        if (tbDeviceDo != null) {
            TbDoorlockPassword tbDoorlockPassword = new TbDoorlockPassword();
            tbDoorlockPassword.setDeviceNo(tbDeviceDo.getDeviceNo());
            tbDoorlockPassword.setThirdPasswordId(passwordId);
            tbDoorlockPassword = doorLockPasswordDataAccess.selectTbDoorlockPassword(tbDoorlockPassword);
            TbDoorlockPassword tbDoorlockPasswordNow = new TbDoorlockPassword();
            tbDoorlockPasswordNow.setId(tbDoorlockPassword.getId());
            tbDoorlockPasswordNow.setUpdateTime(LocalDateTime.now());
            if (errNo.equals("1000")) {
                //操作成功，状态改为无效
                tbDoorlockPasswordNow.setStatus(0);
            } else {
                //操作失败，状态改为正常
                tbDoorlockPasswordNow.setStatus(1);
            }
            doorLockPasswordDataAccess.updateByPrimaryKeySelective(tbDoorlockPasswordNow);
        }
    }

    /**
     * 云丁事件处理
     *
     * @param yunDingDeviceEventReqVO
     */
    @Override
    public void disposeEvent(YunDingDeviceEventReqVO yunDingDeviceEventReqVO) throws Exception {
        String event = yunDingDeviceEventReqVO.getEvent();
        String uuid = yunDingDeviceEventReqVO.getUuid();
        JSONObject detail = yunDingDeviceEventReqVO.getDetail();
        switch (event) {
            //电表用量同步事件
            case "elemeterPowerAsync":
                this.disposeElemeterPowerAsync(uuid,detail);
                break;
            //设备离线事件，包括门锁、采集器、zigbee电表的离线事件。出于降低功耗考虑，门锁心跳间隔 较长，因此门锁实际离线时间可能会比门锁离线事件早半个钟头左右。detail中无内容
            case "lockOfflineAlarm":
                this.disposDeviceOnlineStatus(uuid, 0);
                break;
            //设备解除离线事件，包括门锁、采集器、zigbee电表的离线事件。detail中无内容。
            case "clearLockOfflineAlarm":
                this.disposDeviceOnlineStatus(uuid, 1);
                break;
            //门锁电量事件
            case "batteryAsync":
                LOGGER.info("云丁门锁电量事件，请求参数：{}", JSONObject.toJSONString(yunDingDeviceEventReqVO));
                this.disposeDoorLockPower(uuid, detail);
                break;
            //门锁低电量事件
            case "batteryAlarm":
                LOGGER.info("云丁门锁低电量事件，请求参数：{}", JSONObject.toJSONString(yunDingDeviceEventReqVO));
                this.disposeDoorLockPowerLow(uuid, detail);
                break;
            //门锁解除低电量事件
            case "clearBatteryAlarm":
                LOGGER.info("云丁门锁解除低电量事件，请求参数：{}", JSONObject.toJSONString(yunDingDeviceEventReqVO));
                this.disposeDoorLockPowerLow(uuid, detail);
                break;
            //门锁开锁事件
            case "lockerOpenAlarm":
                LOGGER.info("云丁门锁开锁事件，请求参数：{}", JSONObject.toJSONString(yunDingDeviceEventReqVO));
                this.disposeDoorLockOpen(uuid, detail);
                break;
            //门锁历史开锁事件
            case "lockOpenHistoryAlarm":
                LOGGER.info("云丁门锁历史开锁事件，请求参数：{}", JSONObject.toJSONString(yunDingDeviceEventReqVO));
                this.disposeDoorLockOpenHistory(detail);
                break;
            //水表读数更新事件
            case "watermeterAmountAsync":
                this.disposeWatermeterAmountAsync(uuid, detail.getDouble("amount"));
                break;
            default:
                break;
        }
    }

    /**
     * 云丁水表-电量同步事件操作
     *
     * @param uuid
     * @param amount
     */
    private void disposeWatermeterAmountAsync(String uuid, Double amount) throws Exception {
        TbDeviceDosageCallbackModel tbDeviceDosageCallbackModel = new TbDeviceDosageCallbackModel();
        Double readNumber = amount;

        TbDeviceDosageDo tbDeviceDosageDo = new TbDeviceDosageDo();
        Date currentTime = new Date();
        tbDeviceDosageDo.setCreateTime(LocalDateTime.now());
        tbDeviceDosageDo.setDosageDate(LocalDate.now());
        tbDeviceDosageDo.setEndAmount(readNumber);

        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        tbDeviceDo.setDeviceThirdNo(uuid);

        tbDeviceDosageCallbackModel.setTbDeviceDosageDo(tbDeviceDosageDo);
        tbDeviceDosageCallbackModel.setTbDeviceDo(tbDeviceDo);

        localCallbackService.disposeDeviceDosage(tbDeviceDosageCallbackModel);
    }

    /**
     * 云丁电表-电量同步事件操作
     *
     * @param uuid
     * @param detail
     */
    private void disposeElemeterPowerAsync(String uuid, JSONObject detail) throws Exception {
        TbDeviceDosageCallbackModel tbDeviceDosageCallbackModel = new TbDeviceDosageCallbackModel();

        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setDeviceThirdNo(uuid);
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        TbDeviceDo currentDevice = deviceDataAccess.getTbDeviceByConditon(tbDeviceDo);
        if (currentDevice != null) {
            Double consume_amount = detail.getDouble("consume_amount");
            Date consume_amount_time = detail.getDate("consume_amount_time");
            TbDeviceDosageDo tbDeviceDosageDo = new TbDeviceDosageDo();
            Date date = new Date();
            tbDeviceDosageDo.setCreateTime(LocalDateTime.now());
            tbDeviceDosageDo.setDosageDate(consume_amount_time.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
            tbDeviceDosageDo.setEndAmount(consume_amount);
            tbDeviceDosageDo.setDeviceNo(tbDeviceDo.getDeviceNo());
            tbDeviceDosageCallbackModel.setTbDeviceDosageDo(tbDeviceDosageDo);
            tbDeviceDosageCallbackModel.setTbDeviceDo(tbDeviceDo);
            localCallbackService.disposeDeviceDosage(tbDeviceDosageCallbackModel);
            //调用电表服务刷新-获取最新的电量和分摊电量
            /*ammeterFeginService.refreshAmmeterInfo(new AmmeterInfoReqModel().setDeviceNo(deviceNo));*/
        }

    }

    /*
     * 云丁-设备上下线通知
     *
     * @param uuid
     * @param onlineStatus
     */
    private void disposDeviceOnlineStatus(String uuid, int onlineStatus) throws Exception {
        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setDeviceThirdNo(uuid);
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        tbDeviceDo.setOnlineStatus(onlineStatus);
        //处理水滴IOT本地更新数据业务
        localCallbackService.updateTbDeviceRecord(tbDeviceDo);
    }

    /**
     * 云丁-门锁低电量处理
     *
     * @param uuid
     * @param detail
     * @throws Exception
     */
    private void disposeDoorLockPowerLow(String uuid, JSONObject detail) throws Exception {
        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setDeviceThirdNo(uuid);
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        tbDeviceDo = deviceDataAccess.getTbDeviceByConditon(tbDeviceDo);
        tbDeviceDo.setBatteryLevel(detail.getDouble("battery_level"));
        tbDeviceDo.setReportTime(LocalDateTime.now());
        deviceDataAccess.updateTbDeviceRecord(tbDeviceDo);
    }

    /**
     * 云丁-门锁电量处理
     *
     * @param uuid
     * @param detail
     * @throws Exception
     */
    private void disposeDoorLockPower(String uuid, JSONObject detail) throws Exception {
        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setDeviceThirdNo(uuid);
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        tbDeviceDo = deviceDataAccess.getTbDeviceByConditon(tbDeviceDo);
        tbDeviceDo.setBatteryLevel(detail.getDouble("battery"));
        tbDeviceDo.setReportTime(LocalDateTime.now());
        deviceDataAccess.updateTbDeviceRecord(tbDeviceDo);
    }

    /**
     * 云丁-门锁开门记录
     *
     * @param uuid
     * @param detail
     * @throws Exception
     */
    private void disposeDoorLockOpen(String uuid, JSONObject detail) throws Exception {
        TbDeviceDo tbDeviceDo = new TbDeviceDo();
        tbDeviceDo.setDeviceThirdNo(uuid);
        tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
        tbDeviceDo = deviceDataAccess.getTbDeviceByConditon(tbDeviceDo);
        JSONObject openRecord = new JSONObject();
        //密码ID
        openRecord.put("userId", detail.getString("sourceid"));
        openRecord.put("recordTime", new Date());
        openRecord.put("opType", "tenantPwd");//普通密码开锁
        TbDeviceDosageCallbackModel tbDeviceDosageCallbackModel = new TbDeviceDosageCallbackModel();
        tbDeviceDosageCallbackModel.setOpenRecord(openRecord);
        tbDeviceDosageCallbackModel.setTbDeviceDo(tbDeviceDo);
        localCallbackService.disposeOpenRecordCallback(tbDeviceDosageCallbackModel);
    }

    /**
     * 云丁-门锁历史开门记录
     * 门锁在离线情况下开门，当设备恢复在线后，会上传历史开门事件
     *
     * @param detail
     * @throws Exception
     */
    private void disposeDoorLockOpenHistory(JSONObject detail) throws Exception {
        JSONArray jsonArray = detail.getJSONArray("events");
        JSONObject jsonObject = null;
        TbDeviceDo tbDeviceDo = null;
        TbDeviceDosageCallbackModel tbDeviceDosageCallbackModel = null;
        JSONObject openRecord = null;
        for (int i = 0; i < jsonArray.size(); i++) {
            jsonObject = jsonArray.getJSONObject(i);
            tbDeviceDo = new TbDeviceDo();
            tbDeviceDo.setDeviceThirdNo(jsonObject.getString("uuid"));
            tbDeviceDo.setBrandNo(BrandEnum.YUNDING.getBrand());
            tbDeviceDo = deviceDataAccess.getTbDeviceByConditon(tbDeviceDo);
            openRecord = new JSONObject();
            //密码ID
            openRecord.put("userId", jsonObject.getString("sourceid"));
            openRecord.put("recordTime", new Date(jsonObject.getLong("lock_time")));
            openRecord.put("opType", "tenantPwd");//普通密码开锁
            tbDeviceDosageCallbackModel = new TbDeviceDosageCallbackModel();
            tbDeviceDosageCallbackModel.setOpenRecord(openRecord);
            tbDeviceDosageCallbackModel.setTbDeviceDo(tbDeviceDo);
            localCallbackService.disposeOpenRecordCallback(tbDeviceDosageCallbackModel);
        }
    }

}
